//SUMMARY:  This do file merges our survey data with contextual data for Ballard-Rosa, Malik, Rickard & Scheve (2021)

clear all
set more off

//Set directory
cd "C:\Users\cambr\Dropbox\Conjoint Taxation\UK study\AuthoritarianDraft\Submissions\CPS\Replication materials\Data\"

use "UK full survey (indiv level).dta", clear

//We first merge in data on % non-UK born:
sort postcode_outward
merge m:1 postcode_outward using "Regional data\Non-UK born percent (1991, 2001, 2011).dta"
//NOTE: there are 57 unmatched respondents from Northern Ireland without matches:
drop if _merge == 2
drop _merge

//For Scottish respondents, need separate merge:
replace pcnuk1991 = . if pcnuk1991 == 0
replace pcnuk2001 = . if pcnuk2001 == 0
replace pcnuk2011 = . if pcnuk2011 == 0

//Next merge in inequality and employment data:
sort postcode_outward
merge m:1 postcode_outward using "Regional data\Inequality & Employment data (2002-2016).dta"
//NOTE: there are 57 unmatched respondents from Northern Ireland without matches:
drop if _merge == 2
drop _merge

//Next we merge in manufacturing employment data for 1991, 2000, & 2007
sort postcode_outward
merge m:1 postcode_outward using "Regional data\Manuf employment data (1991, 2000, 2007).dta"
//NOTE: there are 57 unmatched respondents from Northern Ireland without matches:
drop if _merge == 2
drop _merge

//Next we merge in manufacturing employment data for 2015
sort postcode_outward
merge m:1 postcode_outward using "Regional data\Manuf employment data (2015).dta"
//NOTE: there are 57 unmatched respondents from Northern Ireland without matches, plus 3 others
drop if _merge == 2
drop _merge

//Next we merge in Scottish census data for 1991
sort postcode_outward
merge m:1 postcode_outward using "Regional data\Scottish census (1991).dta"
//NOTE: there are 160 matched respondents in Scotland
drop if _merge == 2
drop _merge

replace pcnuk1991 = pcnuk1991_scotland if pcnuk1991_scotland != .
drop pcnuk1991_scotland

//Next we merge in Scottish census data for 2001
sort postcode_outward
merge m:1 postcode_outward using "Regional data\Scottish census (2001).dta"
//NOTE: there are 160 matched respondents in Scotland
drop if _merge == 2
drop _merge

replace pcnuk2001 = pcnuk2001_scotland if pcnuk2001_scotland != .
drop pcnuk2001_scotland

//Next we merge in Scottish census data for 2011
sort postcode_outward
merge m:1 postcode_outward using "Regional data\Scottish census (2011).dta"
//NOTE: there are 160 matched respondents in Scotland
drop if _merge == 2
drop _merge

replace pcnuk2011 = pcnuk2011_scotland if pcnuk2011_scotland != .
drop pcnuk2011_scotland

//Next we merge in immigration data for 2013-2016:
sort postcode_outward
merge m:1 postcode_outward using "Regional data\Immigration data (2013-2016).dta"
//NOTE: there are 57 unmatched respondents from Northern Ireland without matches:
drop if _merge == 2
drop _merge

//Next merge in urban/rural pop data for 2011:
sort postcode_outward
merge m:1 postcode_outward using "Regional data\Urban-rural pop data (2011).dta"
//****NOTE:  DO NOT HAVE URBAN/RURAL FOR SCOTLAND (159 Rs)****
drop if _merge == 2
drop _merge

//Next merge in total pop, and pop change, data for 1991, 2007, 2015:
sort postcode_outward
merge m:1 postcode_outward using "Regional data\Total pop data (1991, 2007, 2011).dta"
//NOTE: there are 57 unmatched respondents from Northern Ireland without matches:
drop if _merge == 2
drop _merge

//Note: There are a few cases where % non-UK was actually zero, but merged as "missing" -- we fix that
replace pcnuk2015 = 0 if pcnuk2015 == .
gen chg_immig = pcnuk2015 - pcnuk2001

//Next we merge in information on respondent postcodes over time:
merge 1:1 caseid using "Respondent postcodes over time.dta"
//Perfect merge
drop _merge

//We need to account for possibility that respondents have moved to different postcodes over time, according to YouGov data:
gen changedPC = 0
gen pc = pc_outward1
forval i = 1/24 {
	disp "i == `i'"
	replace changedPC = 1 if pc_outward`i' != "" & pc != pc_outward`i'
}
drop pc

drop pc_outward* 

//Add some labels
label var pcnuk2015 "% non-UK born"
label var chg_immig "$\Delta$ % non-UK born"

//And save:
save "UK full survey (indiv, with contextual data).dta", replace

disp "RUN NEXT:  CPS replication"
